package net.cyndeline.componentSystem.gui

import net.cyndeline.entitySystem.AbstractEventHandlerSystem
import net.cyndeline.entitySystem.defaultImpl.Aspect
import net.cyndeline.components.objectProperties.MapKey
import com.escalatesoft.subcut.inject.BindingModule
import com.escalatesoft.subcut.inject.Injectable
import net.cyndeline.entitySystem.EntityRef
import net.cyndeline.entitySystem.Event
import net.cyndeline.GUI.MapModifier
import net.cyndeline.components.world.OnMap
import net.cyndeline.events.movement.Movement

class ObjectMovedSystem(implicit val bindingModule: BindingModule)
	extends AbstractEventHandlerSystem(
      new Aspect().getAll(classOf[MapKey], classOf[OnMap]), classOf[Movement]) with Injectable {
  
  override def onEvent(event: Event, entity: EntityRef) = event match {
    case move: Movement => {
      val mapModifier = inject[MapModifier]
      val mapComp = entity.getComponent(classOf[OnMap]).get
      val map = mapComp.map
      val startX = move.start._1
      val startY = move.start._2
      val stopX = move.stop._1
      val stopY = move.stop._2
      val entitiesOnOldPosition = map.objectsAt(startX, startY)
      val entitiesOnNewPosition = map.objectsAt(stopX, stopY)
      
      mapModifier.moveObject(entity, (startX, startY), (stopX, stopY), entitiesOnNewPosition, entitiesOnOldPosition)
    }
    case _ => {}
  }
}